package ml;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;

import corpus.SentPolarityCorpus;

import parser.ParserAnnotation;
import parser.ParserPipeline;

import forum.ForumPost;
import forum.ForumSentence;

import ml.features.FeatureExtractor;

public class BagDataModel extends DataModel {

	public BagDataModel(String name, ParserPipeline pp, String[] labels) {
		super(name, pp, labels);
	}

	public void extractFeatures(SentPolarityCorpus spc) {
		for (Entry<String, String> e : spc.getCorpus().entrySet()) {
			ParserAnnotation pa = spc.getParsedPA(e.getKey(), pp);
			FeatureVector fv = extract(pa, 0);
			fv.setLabel(labelIndex(e.getValue()));
			data.add(fv);
		}
	}

	private FeatureVector extract(ParserAnnotation pa, int sentence) {
		List<Data> data = new ArrayList<Data>();
		for (FeatureExtractor f : features) {
			data.addAll(f.extract(pa, sentence));
		}
		return new FeatureVector(data);
	}

	public void classify(ForumPost post) throws Exception {
		for (int i = 0; i < post.getParserAnnotation().getNbrSentences(); i++) {
			FeatureVector fv = extract(post.getParserAnnotation(), i);
			String str = glue.classify(fv.getData());
			ForumSentence fs = post.getSentences().get(i);
			str = labelTranslations.get(Integer.parseInt(str));
			fs.setEmotion(str);
		}
	}

	public List<String> wekaDescription() {
		List<String> description = new ArrayList<String>();
		for (FeatureExtractor fe : features) {
			description.addAll(Arrays.asList(fe.wekaDescription()));
		}
		String labels = "LABELS {";
		for (int i = 0; i < labelTranslations.size(); i++) {
			if (i == labelTranslations.size()-1)
				labels += i + "}\n";
			else
				labels += i + ",";
		}
		description.add(labels);
		return description;
	}
}
